/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.core.entity;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.persistence.BaseEntity;
import com.intelligent.ispc.utils.RoleEnable;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.List;

/**
 * Update by Rovey.Luo on 2015/10/25.
 */
@Entity
@Table(name = "t_role")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends BaseEntity {
    private String name;
    private String description;

    @Enumerated(EnumType.STRING)
    private RoleEnable enabled;

    @ManyToMany(
            targetEntity = User.class,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            mappedBy = "roles"
    )
    private List<User> users = Lists.newArrayList();

    @ManyToMany(cascade = {CascadeType.PERSIST}, fetch = FetchType.EAGER, targetEntity = Resource.class)
    @JoinTable(
            name = "tr_role_resource",
            joinColumns = @JoinColumn(name = "role_id"),
            inverseJoinColumns = @JoinColumn(name = "resource_id")
    )
    private List<Resource> resources = Lists.newArrayList();

    public List<Resource> getResources() {
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public RoleEnable getEnabled() {
        return enabled;
    }

    public void setEnabled(RoleEnable enabled) {
        this.enabled = enabled;
    }
}
